
#  V Appendix table A3 : Sample Summary Stats -------------------------
A1_appendix <- function(data_set,data,title) {
  data_set[, 
           .(Sample = title,
             num_obs = .N,
             fraction = .N/data[,.N],
             MR_365d = mean(DMG_died_within_365d == "1"),
             med_age = median(DMG_age, na.rm = T),
             f_cost_unadj = sum(UTL_f365d_total_cost/12, na.rm = T)/.N,
             f_cost_adj   = (sum(UTL_f365d_total_cost, na.rm = T)/sum(num_days_lived))*366/12,
             pct_80  = quantile(prob_for_report, c(.80)),
             pct_95  = quantile(prob_for_report, c(.95)),
             pct_99  = quantile(prob_for_report, c(.99)),
             shr_M_80p = sum(DMG_died_within_365d == "1" & prob_for_report > 0.8) /
               sum(DMG_died_within_365d == "1"))
           ]
}

do_table_mainTopo_descStats <- function(data_all = dt_for_exhibits_all,
                        data_cnr = dt_for_exhibits_cancer) { 
  apx_table <- rbind(
    A1_appendix(data_cnr,data_cnr,"All"),
    A1_appendix(data_cnr[DMG_age>65],data_cnr,"Age>65"),
    A1_appendix(data_cnr[DMG_age>85],data_cnr,"Age>85"),
    # By cancer types : 
    data_cnr[, 
                           .(num_obs = .N,
                             fraction = .N/data_cnr[,.N],
                             MR_365d = mean(DMG_died_within_365d == "1"),
                             med_age = median(DMG_age, na.rm = T),
                             f_cost_unadj = sum(UTL_f365d_total_cost/12, na.rm = T)/.N,
                             f_cost_adj   = (sum(UTL_f365d_total_cost, na.rm = T)/sum(num_days_lived))*366/12,
                             pct_80  = quantile(prob_for_report, c(.80)),
                             pct_95  = quantile(prob_for_report, c(.95)),
                             pct_99  = quantile(prob_for_report, c(.99)),
                             shr_M_80p = sum(DMG_died_within_365d == "1" & prob_for_report > 0.8) /
                               sum(DMG_died_within_365d == "1")),
                           by = .(Sample= CNRS_topo_main_groups)
                           ][order(num_obs, decreasing = T)][
                             ,last:=ifelse(Sample %in% c("Others","Unknown primary site"),1,0) ][
                               order(last , -num_obs)][,last:=NULL]
  )[          
    , c("fraction","MR_365d", "pct_80", "pct_95", "pct_99", "shr_M_80p") := lapply(.SD, function(x){round(x*100,1)}), 
    .SD = c("fraction","MR_365d", "pct_80", "pct_95", "pct_99", "shr_M_80p")
    ][
      , `:=` (num_obs = scales::comma(num_obs),
              med_age = round(med_age),
              f_cost_unadj = scales::comma(round(f_cost_unadj)),
              f_cost_adj   = scales::comma(round(f_cost_adj)))
      ]
  
  apx_table[ Sample == "Lymph nodes (secondary?)", Sample := "Lymph nodes" ][
    Sample == "Others", Sample := "Other" ][
      , Sample := tools::toTitleCase(Sample)]
  
  
  invisible(Hmisc::latex(
    apx_table,
    file = paste0("appendix_table_mainTopo_descStats.tex"),
    # rowname = NULL,
    center = 'centering',
    n.cgroup = c(1, 2, 1, 1, 2, 3, 1),
    cgroup = c("Sample", "\\makecell{Number\\\\of Observations}", "\\makecell{Mortality\\\\Rate}", 
               "\\makecell{Age\\\\(Median)}",
               "\\makecell{Total Spending\\\\(NIS, 365 Days After)}", 
               "\\makecell{Percentiles of\\\\Predicted Mortality}", 
               "\\makecell{Share of Dead\\\\ With Pred. \\\\Mort. gt 0.8}"),
    colheads = c("", "N","\\makecell{Fraction\\\\of Sample}", "", "", "Unadjusted", "\\makecell{Adjusted\\\\for Survival}",
                 "80th", "95th", "99th", ""),
    n.rgroup = c(3, 21),
    rgroup   = c("A. All Cancer Types",
                 "B. By Cancer Type"),
    rowname = c(rep("",24)),
    rowlabel = "",
    col.just = c("l", rep.int("r", 10)),
    extracolheads = c("","(1)", "(2)", "(3)", "(4)","(5)","(6)", "(7)", "(8)", "(9)","(10)"), 
    extracolsize = "normalsize"
  ))
  
  return(apx_table)
}

do_table_mainTopo_descStats_revis <- function(data_all = dt_for_exhibits_all,
                                        data_cnr = dt_for_exhibits_cancer,
                                        title) { 
  apx_table <- rbind(
    A1_appendix(data_cnr,data_cnr,"All"),
    A1_appendix(data_cnr[DMG_age>65],data_cnr,"Age>65"),
    A1_appendix(data_cnr[DMG_age>85],data_cnr,"Age>85"),
    # By cancer types : 
    data_cnr[, 
             .(num_obs = .N,
               fraction = .N/data_cnr[,.N],
               MR_365d = mean(DMG_died_within_365d == "1"),
               med_age = median(DMG_age, na.rm = T),
               f_cost_unadj = sum(UTL_f365d_total_cost/12, na.rm = T)/.N,
               f_cost_adj   = (sum(UTL_f365d_total_cost, na.rm = T)/sum(num_days_lived))*366/12,
               pct_80  = quantile(prob_for_report, c(.80)),
               pct_95  = quantile(prob_for_report, c(.95)),
               pct_99  = quantile(prob_for_report, c(.99)),
               shr_M_80p = sum(DMG_died_within_365d == "1" & prob_for_report > 0.8) /
                 sum(DMG_died_within_365d == "1")),
             by = .(Sample= CNRS_topo_main_groups)
             ][order(num_obs, decreasing = T)][
               ,last:=ifelse(Sample %in% c("Others","Unknown primary site"),1,0) ][
                 order(last , -num_obs)][,last:=NULL]
  )[          
    , c("fraction","MR_365d", "pct_80", "pct_95", "pct_99", "shr_M_80p") := lapply(.SD, function(x){round(x*100,1)}), 
    .SD = c("fraction","MR_365d", "pct_80", "pct_95", "pct_99", "shr_M_80p")
    ][
      , `:=` (num_obs = scales::comma(num_obs),
              med_age = round(med_age),
              f_cost_unadj = scales::comma(round(f_cost_unadj)),
              f_cost_adj   = scales::comma(round(f_cost_adj)))
      ]
  
  apx_table[ Sample == "Lymph nodes (secondary?)", Sample := "Lymph nodes" ][
    Sample == "Others", Sample := "Other" ][
      , Sample := tools::toTitleCase(Sample)]
  
  
  invisible(Hmisc::latex(
    apx_table,
    file = paste0("table_mainTopo_descStats",title,".tex"),
    # rowname = NULL,
    center = 'centering',
    n.cgroup = c(1, 2, 1, 1, 2, 3, 1),
    cgroup = c("Sample", "\\makecell{Number\\\\of Observations}", "\\makecell{Mortality\\\\Rate}", 
               "\\makecell{Age\\\\(Median)}",
               "\\makecell{Total Spending\\\\(NIS, 365 Days After)}", 
               "\\makecell{Percentiles of\\\\Predicted Mortality}", 
               "\\makecell{Share of Dead\\\\ With Pred. \\\\Mort. gt 0.8}"),
    colheads = c("", "N","\\makecell{Fraction\\\\of Sample}", "", "", "Unadjusted", "\\makecell{Adjusted\\\\for Survival}",
                 "80th", "95th", "99th", ""),
    n.rgroup = c(3, 21),
    rgroup   = c("A. All Cancer Types",
                 "B. By Cancer Type"),
    rowname = c(rep("",24)),
    rowlabel = "",
    col.just = c("l", rep.int("r", 10)),
    extracolheads = c("","(1)", "(2)", "(3)", "(4)","(5)","(6)", "(7)", "(8)", "(9)","(10)"), 
    extracolsize = "normalsize"
  ))
  
  return(apx_table)
}
